package edu.njust.cma.util;

import edu.njust.cma.DTO.Excel04;
import edu.njust.cma.pojo.Equipment;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.util.List;

public class ExcelUtil04 {
    // 生成Excel文件的方法
    public static XSSFWorkbook createExcelFile(List<Excel04> dataList) {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Data");

        // 创建单元格样式并设置边框
        XSSFCellStyle borderStyle = workbook.createCellStyle();
        borderStyle.setBorderTop(BorderStyle.THIN);
        borderStyle.setBorderBottom(BorderStyle.THIN);
        borderStyle.setBorderLeft(BorderStyle.THIN);
        borderStyle.setBorderRight(BorderStyle.THIN);

        // 创建表头
        XSSFRow headerRow1 = sheet.createRow(0);

        headerRow1.createCell(0).setCellValue("大类名称");
        headerRow1.createCell(1).setCellValue("类别(产品/项目/参数)");
        headerRow1.createCell(2).setCellValue("产品/项目/参数");
        headerRow1.createCell(3).setCellValue("依据的标准（方法）");
        headerRow1.createCell(5).setCellValue("限制范围");
        headerRow1.createCell(6).setCellValue("说明");
        headerRow1.createCell(7).setCellValue("授权签字人姓名");
        headerRow1.createCell(8).setCellValue("身份证号");

        XSSFRow headerRow2 = sheet.createRow(1);
        headerRow2.createCell(2).setCellValue("名称");
        headerRow2.createCell(3).setCellValue("名称");
        headerRow2.createCell(4).setCellValue("编号（含年号）");

        // 添加表头
        XSSFRow headerRow3 = sheet.createRow(2);

        // 合并表头单元格
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 3, 4));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 1));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 5, 5));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 6, 6));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 7, 7));
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 8, 8));

        // 填充数据
        int rowNum = 2;
        for (Excel04 data : dataList) {
            XSSFRow row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(data.getMajorCategoryName());
            row.createCell(1).setCellValue(data.getMinorCategoryName());
            row.createCell(2).setCellValue(data.getTaskName());
            //System.out.println(data.getTaskName());
            row.createCell(3).setCellValue(data.getStandardName());
            row.createCell(4).setCellValue(data.getStandardNumber());


        }
        return workbook;
    }
}
